package cn.stylefeng.guns.modular.recy.utils;

import com.alibaba.fastjson.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AjaxResult
{
    private Integer error_code;
    private String msg;
    private List<Map<String, String>> data;
    private List<Object> result;
    private Integer code;
    
    public String toJson() {
        return JSON.toJSONString(this);
    }
    
    public String toHtml() {
        final String html = "<!doctype html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\"\n          content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\">\n    <title>\u63d0\u793a</title>\n</head>\n<body>\n<div style=\"width:100vw;height:100vh;display:flex;align-items: center;justify-content:center;text-align:center\"><div style='width:80vw;color:white;background:#009688;text-align:center;padding:1rem'>" + this.msg + "</div</div>\n</body>\n</html>";
        return html;
    }
    
    public Integer getCode() {
        return this.error_code;
    }
    
    public static AjaxResult error(final String msg) {
        return builder().msg(msg).error_code(Integer.valueOf(500)).build();
    }
    
    public static AjaxResult success(final String msg) {
        return builder().msg(msg).error_code(Integer.valueOf(200)).build();
    }
    
    public static String errorHtml(final String msg) {
        return error(msg).toHtml();
    }
    
    public static String errorJson(final String msg) {
        return error(msg).toJson();
    }
    
    public static String successHtml(final String msg) {
        return success(msg).toHtml();
    }
    
    public static String successJson(final String msg) {
        return success(msg).toJson();
    }
    

}
